package com.maxmlr.music.admin.service.transfer;

import com.maxmlr.music.admin.entity.Role;
import com.maxmlr.music.admin.entity.RoleMenu;
import com.maxmlr.music.admin.entity.User;
import com.maxmlr.music.admin.param.RoleParam;
import com.maxmlr.music.common.util.DateUtil;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName RoleTransfer
 * @Description TODO
 * @Author maxiao
 * @Date 2020/6/15 下午2:51
 * @Version V1.0
 */
@Component
public class RoleTransfer {


    public Role po2Role(RoleParam param, User adminUser) {
        Date date = DateUtil.getDate();
        Role role = new Role();
        role.setRoleName(param.getRoleName());
        role.setRoleSign(param.getRoleSign());
        role.setRemark(param.getRemark());
        role.setCreateUserId(adminUser.getId());
        role.setCreateTime(date);
        role.setUpdateTime(date);
        return role;
    }

    public List<RoleMenu> po2RoleMenus(List<Integer> menuIds, Integer id) {
        return menuIds.stream()
                .filter(p -> Objects.nonNull(p) && p > 0)
                .map(p -> {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(id);
                    roleMenu.setMenuId(p);
                    return roleMenu;
                }).collect(Collectors.toList());
    }

    public void fullRole(Role role, RoleParam param, User adminUser) {
        Date date = DateUtil.getDate();
        role.setRoleName(param.getRoleName());
        role.setRoleSign(param.getRoleSign());
        role.setRemark(param.getRemark());
        role.setCreateUserId(adminUser.getId());
        role.setUpdateTime(date);
    }
}
